package com.ruoyi.period.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.period.domain.Client;
import com.ruoyi.period.domain.Employees;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.period.domain.Myclass;
import com.ruoyi.period.service.IMyclassService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 课期Controller
 *
 * @author ruoyi
 * @date 2021-11-05
 */
@RestController
@RequestMapping("/myclass")
public class MyclassController extends BaseController {
    @Autowired
    private IMyclassService myclassService;

    /**
     * 查询课期列表
     */
    @RequiresPermissions("period:myclass:list")
    @GetMapping("/list")
    public TableDataInfo list(Myclass myclass) {
        startPage();
        List<Myclass> list = myclassService.selectMyclassList(myclass);
        return getDataTable(list);
    }


    /**
     * 导出课期列表
     */
    @RequiresPermissions("period:myclass:export")
    @Log(title = "课期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Myclass myclass) throws IOException {
        List<Myclass> list = myclassService.selectMyclassList(myclass);
        ExcelUtil<Myclass> util = new ExcelUtil<Myclass>(Myclass.class);
        util.exportExcel(response, list, "课期数据");
    }

    /**
     * 获取课期详细信息
     */
    @RequiresPermissions("period:myclass:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(myclassService.selectMyclassById(id));
    }

    /**
     * 新增课期
     */
    @RequiresPermissions("period:myclass:add")
    @Log(title = "课期", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Myclass myclass, @RequestBody List<Integer> clientIdList) {
        return toAjax(myclassService.insertMyclass(myclass,clientIdList));
    }

    /**
     * 修改课期
     */
    @RequiresPermissions("period:myclass:edit")
    @Log(title = "课期", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Myclass myclass) {
        return toAjax(myclassService.updateMyclass(myclass));
    }

    /**
     * 删除课期
     */
    @RequiresPermissions("period:myclass:remove")
    @Log(title = "课期", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(myclassService.deleteMyclassByIds(ids));
    }

    /**
     * 显示可选择的助教
     *
     * @param courseId 课程id
     * @return 所有可以选择的助教
     */
    @GetMapping("/listClient/{courseId}")
    public TableDataInfo clientList(@PathVariable("courseId") Long courseId) {
        startPage();
        List<Client> clientList = myclassService.selectClientByCourseId(courseId);
        return getDataTable(clientList);
    }

    /**
     * @return 所有可以选择的班主任
     */
    @GetMapping("/listClient")
    public TableDataInfo employeesList() {
        startPage();
        List<Employees> employeesList = myclassService.selectEmployees();
        return getDataTable(employeesList);
    }


}
